/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2013 All Rights Reserved.
 */
package com.pingan.mobilegw.core.service.invoke.async;

import com.pingan.mobilegw.adapterservice.ResultEnum;
import com.pingan.mobilegw.core.model.GatewayRequest;
import com.pingan.mobilegw.core.model.GatewayResponse;
import com.pingan.mobilegw.core.model.constant.ContextConstant;
import com.pingan.mobilegw.core.service.util.ResponseUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 
 * @author jiadao
 * @version $Id: AsyncListenerImpl.java, v 0.1 2013-8-7 下午3:43:37 jiadao Exp $
 */
public class AsyncListenerImpl implements AsyncListener {

    /** 默认logger */
    private static final Log logger = LogFactory.getLog(AsyncListenerImpl.class);

    @Override
    public void onComplete(AsyncEvent event) throws IOException {
        //LoggerUtil.debug(logger, "async request complete , requesId=", parseRequestId(event));
    }

    /** 
     * @see javax.servlet.AsyncListener#onTimeout(javax.servlet.AsyncEvent)
     */
    @Override
    public void onTimeout(AsyncEvent event) throws IOException {
        AsyncContext asyncContext = event.getAsyncContext();
        HttpServletRequest httpRequest = (HttpServletRequest) asyncContext.getRequest();

        GatewayResponse response = ResponseUtil.buildErrorResponse(ResultEnum.RequestTimeOut);

        // 解析requestId
        String requestId = (String) httpRequest.getAttribute(ContextConstant.REQUEST_ID);
        GatewayRequest request = Async.context.remove(requestId);
        if (request == null) {
            //LoggerUtil.error(logger, null, "async request timeout , requesId=", requestId);
            return;
        }

        // 执行pipe
//        DefaultPipeline.doUpstreamPipe(request, response);

        // complete response
        asyncContext.complete();
    }

    /** 
     * @see javax.servlet.AsyncListener#onError(javax.servlet.AsyncEvent)
     */
    @Override
    public void onError(AsyncEvent event) throws IOException {
     //   LoggerUtil.error(logger, event.getThrowable(), "async request error , requesId=",
       //     parseRequestId(event));
    }

    /** 
     * @see javax.servlet.AsyncListener#onStartAsync(javax.servlet.AsyncEvent)
     */
    @Override
    public void onStartAsync(AsyncEvent event) throws IOException {
        // 不会调用到
    }

    /**
     * 解析请求id
     * 
     * @param event
     * @return
     */
    private String parseRequestId(AsyncEvent event) {
        AsyncContext asyncContext = event.getAsyncContext();
        HttpServletRequest httpRequest = (HttpServletRequest) asyncContext.getRequest();
        return (String) httpRequest.getAttribute(ContextConstant.REQUEST_ID);
    }

}
